package com.fwmagic.flink.tablesql.stream;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.table.api.Slide;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.java.StreamTableEnvironment;
import org.apache.flink.types.Row;


public class SlidlingEventTimeWindowsSQL {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

        StreamTableEnvironment streamTableEnv = StreamTableEnvironment.create(env);


        /**
         * 1000,u1,p1,5
         * 2000,u1,p1,5
         * 2000,u2,p1,3
         * 3000,u1,p1,5
         * 9999,u2,p1,3
         * 19999,u1,p1,5
         */
        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);

        SingleOutputStreamOperator<Row> rowDataStream = lines.map(new MapFunction<String, Row>() {
            @Override
            public Row map(String line) throws Exception {
                String[] fields = line.split(",");
                long time = Long.parseLong(fields[0]);
                String uid = fields[1];
                String pid = fields[2];
                double money = Double.parseDouble(fields[3]);
                return Row.of(time, uid, pid, money);
            }
        }).returns(Types.ROW(Types.LONG, Types.STRING, Types.STRING, Types.DOUBLE));


        //提交数据中的EventTime
        SingleOutputStreamOperator<Row> waterMarkRowsDataStream = rowDataStream.assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<Row>(Time.seconds(0)) {
            @Override
            public long extractTimestamp(Row element) {
                return (long) element.getField(0);
            }
        });

        /**
         * [0,10000)
         * [0,9999]
         * win.start 0
         * wind.end 10000
         * win.rowtime 9999
         */
        streamTableEnv.registerDataStream("t_orders",
                waterMarkRowsDataStream,
                "atime,uid,pid,money,rowtime.rowtime");


        Table table = streamTableEnv.sqlQuery("select uid,sum(money),HOP_END(rowtime,INTERVAL '2' SECOND,INTERVAL '10'SECOND)" +
                " as winEnd from t_orders group by HOP(rowtime,INTERVAL '2' SECOND,INTERVAL '10' SECOND),uid");


        DataStream<Tuple2<Boolean, Row>> tuple2DataStream = streamTableEnv.toRetractStream(table, Row.class);

        tuple2DataStream.print();

        env.execute("SlidlingEventTimeWindowsSQL");

    }
}
